<?php

namespace App\Kuafu\CouponPackage\Validate;

use App\Kuafu\CouponPackage\Enum\CouponPackageEnum;
use App\Kuafu\Base\Validate\BaseValidate;
use Illuminate\Support\Facades\Validator;

class CouponPackageValidate extends BaseValidate
{

    public static function getRules()
    {
        return [
            'shopType'               => 'required|int|in:' . implode(',', array_keys(CouponPackageEnum::SHOP_TYPES)),
            'name'                   => 'required|string|min:1|max:50',
            'startAt'                => 'required|date_format:Y-m-d H:i:s',
            'endAt'                  => 'required|string|date_format:Y-m-d H:i:s|compareDateTime:startAt',
            'salePrice'              => 'required|int|min:1',
            'dayStartAt'             => 'required|int|min:0|max:235959',
            'dayEndAt'               => 'required|int|min:0|max:235959|gt:dayStartAt',
            'canSaleCount'           => 'required|int|min:1',
            'couponPackageImg'       => 'required|string|max:120',
//             'reachRemind'        => 'required|int|in:' . implode(',', array_keys(CouponPackageEnum::REACH_REMIND_MAP)),
//             'expiredRemind'      => 'required|int|in:' . implode(',', array_keys(CouponPackageEnum::EXPIRED_REMIND_MAP)),
            'miniAdvPopSwitch'       => 'required|int|in:' . implode(',',
                    array_keys(CouponPackageEnum::MINI_ADV_POP_SWITCH_MAP)),
            'miniAdvPopImg'          => 'exclude_unless:miniAdvPopSwitch,' . CouponPackageEnum::MINI_ADV_POP_SWITCH_ON . '|required|string|max:120',
            'bannerImg'              => 'required|string|max:120',
            'detailImg'              => 'required|string|max:120',
            'collectMoneyCopy'       => 'required|string',
            'publicityCopy'          => 'required|string',
            'noticeOfPurchase'       => 'required|string',
            'noticeOfPurchaseDetail' => 'required|string',
            'shareTitle'             => 'required|string|max:120',
            'popDate'                => 'required|array'
        ];
    }


    /**
     * @param array $requestParams
     * @return array
     */
    public static function couponPackageInsertValidate(array $requestParams): array
    {
        $errMsgAll = '';
        list($couponPackageParams, $errMsg) = CouponPackageValidate::insertValidate($requestParams);
        $errMsgAll .= $errMsg;
        list($couponPackageMappingParams, $errMsg) = CouponPackageMappingValidate::insertValidate($requestParams);
        $errMsgAll .= $errMsg;
        list($couponPackageShopMappingParams, $errMsg) = CouponPackageShopMappingValidate::insertValidate($requestParams);
        $errMsgAll                      .= $errMsg;
        $couponPackageParams['popDate'] = implode(',', $couponPackageParams['popDate']);
        return [$couponPackageParams, $couponPackageMappingParams, $couponPackageShopMappingParams, $errMsgAll];
    }

    /**
     * @param array $requestParams
     * @return array
     */
    public static function insertValidate(array $requestParams): array
    {
        Validator::extend('compareDateTime',
            function ($attribute, $value, $parameters, \Illuminate\Validation\Validator $validator) {
                $validator->requireParameterCount(1, $parameters, 'compareDateTime');
                $comparedToValue = $validator->getData()[$parameters[0]];
                return strtotime($value) > strtotime($comparedToValue);
            });

        return [
            self::getRequestParams($requestParams, array_keys(self::getRules())),
            self::validateCheck($requestParams, self::getRules(), [
                'endAt.compare_date_time' => '起始时间&截止售卖时间不合法'
            ])
        ];
    }


}
